using System;
using System.Text.RegularExpressions;

namespace newtelligence.DasBlog.Web.Core
{
    /// <summary>
    /// Summary description for ContentReferralBlacklist.
    /// </summary>
    public class ReferralUrlBlacklist : IBlackList
    {
        private static readonly object blackListLock = new object();
        private static string blacklist = null;
        private static Regex blackListRegex = null;
        private static bool loaded = false;

        #region IBlackList Members

        public void Initialize(string newBlackList)
        {
            lock (blackListLock)
            {
                if (blacklist == null || blacklist != newBlackList)
                {
                    if (newBlackList != null && newBlackList.Length > 0)
                    {
                        blacklist = newBlackList;
                        blacklist = blacklist.Replace(";", "|");
                        blacklist = blacklist.Replace("+", "\\+");
                        blacklist = blacklist.Replace("*", "\\*");
                        blackListRegex =
                            new Regex(blacklist,
                                      RegexOptions.Compiled | RegexOptions.IgnoreCase |
                                      RegexOptions.IgnorePatternWhitespace);
                    }
                    else
                    {
                        throw new NullReferenceException("blacklist string is empty");
                    }
                }
            }

            loaded = true;
        }

        public Match IsBlacklisted(string url)
        {
            if (!loaded)
            {
                return null;
            }

            try
            {
                Match match;
                Uri urlReferrer = new Uri(url);

                // we want to remove the Query from the url as it may contain keywords that easily match
                // the black list. However, we CHECK against the stripped referrer and log the COMPLETE referral!
                string strippedReferrer = urlReferrer.Scheme + "://" + urlReferrer.Authority + urlReferrer.AbsolutePath;

                lock (blackListLock)
                {
                    match = blackListRegex.Match(strippedReferrer);
                }

                return match;
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("An error occured trying to determine if {0} is blacklisted", url),
                                    ex.InnerException);
            }
        }

        public BlacklistUpdateState UpdateBlacklist()
        {
            return BlacklistUpdateState.None;
        }

        #endregion
    }
}